package com.findu.activity;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.easemob.EMCallBack;
import com.easemob.chat.EMChatManager;
import com.example.findu.R;
import com.findu.util.CommonUtils;

/**
 * @Title: LoginActivity.java
 * @Package com.findu.activity
 * @Description: 注册
 * @author ggj
 * @date 2015-10-29 下午3:03:05
 * @version V1.0
 */
public class LoginActivity extends Activity implements OnClickListener {
	private ImageView mImg_head;
	private EditText mEdt_name;
	private EditText mEdt_pwd;
	private Button mLogin;
	private Button mLogin_register;

	// End Of Content View Elements

	private void bindViews() {

		mImg_head = (ImageView) findViewById(R.id.img_head);
		mEdt_name = (EditText) findViewById(R.id.edt_name);
		mEdt_pwd = (EditText) findViewById(R.id.edt_pwd);
		mLogin = (Button) findViewById(R.id.login);
		mLogin_register = (Button) findViewById(R.id.login_register);
		// 登录按钮注册事件
		mLogin.setOnClickListener(this);
		mLogin_register.setOnClickListener(this);
	}
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login);
		bindViews();
		 mEdt_name.setText("13330352893");
		//mEdt_name.setText("***");
		mEdt_pwd.setText("123456");

	}
	
	/**
	 * 登录返回
	 */
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);

		String requestCodes = data.getStringExtra("data_return");
		mEdt_name.setText(requestCodes);
		
		/**
		 * Toast.makeText(LoginActivity.this, "OK" + " " + requestCodes,
		 * Toast.LENGTH_SHORT).show(); switch (requestCode) { case 1:
		 * if(requestCode==RESULT_OK){ String requestCodes =
		 * data.getStringExtra("data_return"); mEdt_name.setText(requestCodes);
		 * Toast.makeText(LoginActivity.this, "OK"+" "+requestCodes,
		 * Toast.LENGTH_SHORT).show(); } break;
		 * 
		 * default: break;
		 * 
		 * }
		 */

	}

	/**
	 * 点击事件处理
	 */
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		// 根据id判断不同的按钮做不同的事情
		case R.id.login:
			// 跳转到主界面方法1
			// Toast.makeText(LoginActivity.this, "登录开始", 0).show();
			/*
			 * Intent intent = new Intent(this, Conversation_home.class);
			 * startActivity(intent);
			 */
			// 跳转到主界面方法2
		   // Conversation_home.start(this);
			 login(mEdt_name.getText().toString(), mEdt_pwd.getText().toString());
			break;

		case R.id.login_register:

			// 跳转到注册界面
			 RegisterActivity.start(this);
			/*Intent intent = new Intent(LoginActivity.this,RegisterActivity.class);
			startActivityForResult(intent, 1);*/
			 
			 
			  //不能关闭不然第三个验证页面不好传#######
			  finish(); 
			  
			break;
		}

	}

	/*
	 * @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the
	 * menu; this adds items to the action bar if it is present.
	 * getMenuInflater().inflate(R.menu.login, menu); return true; }
	 */

	/**
	 * 
	 * @author ggj 跳转到当前页面
	 *         参考资料http://blog.csdn.net/kroclin/article/details/47724283
	 *         #comments
	 */
	public static void start(Context context) {
		Intent intent = new Intent(context, LoginActivity.class);
		context.startActivity(intent);
	}
	
	private boolean login(String account, String password){
		if (!CommonUtils.isNetWorkConnected(this)) {
			Toast.makeText(this, R.string.network_is_inavailable, Toast.LENGTH_SHORT).show();
			return false;
		}
		
		if (TextUtils.isEmpty(account)) {
			Toast.makeText(this, R.string.User_name_cannot_be_empty, Toast.LENGTH_SHORT).show();
			return false;
		}
		if (TextUtils.isEmpty(password)) {
			Toast.makeText(this, R.string.Password_cannot_be_empty, Toast.LENGTH_SHORT).show();
			return false;
		}
		
		final ProgressDialog pd = new ProgressDialog(LoginActivity.this);
		pd.setCanceledOnTouchOutside(false);
		pd.setMessage(getString(R.string.Is_landing));
		pd.show();
		// 登录环信
		EMChatManager.getInstance().login(account, password, new EMCallBack() {

			@Override
			public void onError(int arg0, final String arg1) {
				// TODO Auto-generated method stub
				runOnUiThread(new Runnable() {
					public void run() {
						Log.e("test", "login failed: " + arg1);
						pd.dismiss();
						Toast.makeText(getApplicationContext(), getString(R.string.Login_failed),
								Toast.LENGTH_SHORT).show();
					}
				});
			}

			@Override
			public void onProgress(int arg0, String arg1) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onSuccess() {
				// TODO Auto-generated method stub
				try {
					// ** 第一次登录或者之前logout后再登录，加载所有本地群和回话
					// ** manually load all local groups and
				    //EMGroupManager.getInstance().loadAllGroups();
					EMChatManager.getInstance().loadAllConversations();
					// to do: 处理好友和群组
					
				} catch (Exception e) {
					e.printStackTrace();
					// 取好友或者群聊失败，不让进入主页面
					runOnUiThread(new Runnable() {
						public void run() {
							pd.dismiss();
							Toast.makeText(getApplicationContext(), getString(R.string.Login_failed), 1).show();
						}
					});
					return;
				}
				
				runOnUiThread(new Runnable() {
					public void run() {
						pd.dismiss();
						Conversation_home.start(LoginActivity.this);
						finish();
					}
				});
			}
		});
		
		return true;
	}
}
